//
//  CallInfoViewController.m
//  CallTelphone
//
//  Created by QFX on 2023/5/24.
//

#import "CallInfoViewController.h"
#import "CallEditViewController.h"

@interface CallInfoViewController ()

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UILabel *telphoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *afterLabel;
@property (weak, nonatomic) IBOutlet UILabel *laidian;

@end

@implementation CallInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];
    
    self.telphoneLabel.text = self.model.phone;
    double timeInterval = [self.model.timeInterval doubleValue];
    NSString *dates = [self judgeCurrentdateWithDate:timeInterval];
    
    int totalSeconds = [self.model.totalSeconds intValue];
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    
    self.statusLabel.text = [self.model.isCallIn intValue] == 1 ? @"来电" : @"去电";
    self.dateLabel.text = dates;
    self.timeLabel.text = [self hourTime:timeInterval];
    if (hours > 0 && minutes > 0){
        self.afterLabel.text = [NSString stringWithFormat:@"%d小时%d分钟",hours,minutes];
        return;
    }else if (minutes > 0){
        self.afterLabel.text = [NSString stringWithFormat:@"%d分钟",minutes];
        return;
    }else{
        self.afterLabel.text = [NSString stringWithFormat:@"%d秒钟",seconds];
        return;
    }
}

- (void)edit{
    CallEditViewController *editVC = [[CallEditViewController alloc] initWithNibName:@"CallEditViewController" bundle:nil];
    editVC.model = self.model;
    [self.navigationController pushViewController:editVC animated:YES];
}



- (NSString*)hourTime:(double)timestring{
    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:timestring];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];
    return timeStr;
}

- (NSString *)judgeCurrentdateWithDate:(double)beTime {
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - beTime;
    NSString * distanceStr;

    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];

    [df setDateFormat:@"dd"];
    NSString * nowDay = [df stringFromDate:[NSDate date]];
    NSString * lastDay = [df stringFromDate:beDate];

//    if (distanceTime < 60) {//小于一分钟
//        distanceStr = @"刚刚";
//    }else if (distanceTime <60*60) {//时间小于一个小时
//        distanceStr = [NSString stringWithFormat:@"%ld分钟",(long)distanceTime/60];
//    }else
        
    if(distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]){//时间小于一天
        distanceStr = @"今天";
    } else if(distanceTime<24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
        if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
             distanceStr =  @"昨天";
        } else if ([nowDay integerValue] - [lastDay integerValue] == 2) {
            distanceStr =  @"前天";
        }  else{
            [df setDateFormat:@"yyyy年MM月dd日"];
            distanceStr = [df stringFromDate:beDate];
        }
    } else if(distanceTime <24*60*60*365){
        [df setDateFormat:@"yyyy年MM月dd日"];
        distanceStr = [df stringFromDate:beDate];
    }  else{
        [df setDateFormat:@"yyyy年MM月dd日"];
        distanceStr = [df stringFromDate:beDate];
    }
    return distanceStr;
}
@end
